															



import tkinter as tk
#import tkinter.messagebox as tkMsgBox
import random
top = tk.Tk()
#top.geometry('750x750')
top.resizable(False,False)
frame_list = []
label_list = [[]for i in range(0,51)]
organism_list = []
def loading():
    global frame_list,label_list
    for i in range(0,51):
       frame = tk.Frame()
       frame_list.append(frame)

    for i in range(0,len(frame_list)):
        for j in range(0,101):
            lab = tk.Label(frame_list[i],text = '',width = 1,height = 1,bg = '#009696' )
            label_list[i].append(lab)
#print('-------------------------------------------------------------')

    for i in range(0,len(frame_list)):
        frame_list[i].pack()

#print('-------------------------------------------------------------')

    for i in range(0,len(label_list)):
        for j in range(0,len(label_list[i])):
            label_list[i][j].pack(side = tk.LEFT)

#print('-------------------------------------------------------------')

#print(frame_list)
#print(label_list)
def board():
    global label_list
    num = random.randint(1,800)
    for i in range(0,num):
        x = random.randint(0,50)
        y = random.randint(0,100)
        organism_list.append((x,y))
        label_list[x][y].configure(bg = '#c17538')
        
def nice(living_num):
    if living_num == 2 or living_num == 3:
        return True
    return False


def survive():
    global organism_list,label_list
    for x in range(0,101):
        for y in range(0,51):
            livingnum = 0
            for i in range(x - 1,x + 1):
                for j in range(y - 1,y + 1):
                    if x - 1 < 0 or x + 1 > 50 or y - 1 < 0 or y + 1 > 100:
                        continue
                    if label_list[i][j] in organism_list:
                        livingnum += 1
            if nice(livingnum) == True:
                if not (x,y) in organism_list:
                    organism_list.append((x,y))
            else:
                if (x,y) in organism_list:
                    organism_list.remove((x,y))
            
            
def mark():
    global label_list
    for i in organism_list:   
        label_list[i[0]][i[1]].configure(bg = '#c17538')

def main():
    loading()
    board()
    while True:    
        survive()
        mark()
    
        
main()
tk.mainloop()